package com.briup.srms.exam.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.srms.exam.bean.QuestionBank;
import com.briup.srms.exam.dao.QuestionBankMapper;
import com.briup.srms.exam.dao.extend.QuestionBankExtendMapper;
import com.briup.srms.exam.service.IQuestionBankService;
import com.jacky.base.utils.CustomerException;
import com.jacky.base.utils.PageVM;

@Service
public class QuestionBankServiceImpl implements IQuestionBankService {
	@Autowired
	private QuestionBankMapper questionBankMapper;
	@Autowired
	private QuestionBankExtendMapper questionBankExtendMapper;
	
	@Override
	public PageVM<QuestionBank> pageQuery(int page, int pageSize) {
		List<QuestionBank> list = questionBankExtendMapper.query(page, pageSize);
		long total = questionBankExtendMapper.count();
		return new PageVM<>(page, pageSize, total, list);
	}
	
	@Override
	public void saveOrUpdate(QuestionBank questionBank) {
		if(questionBank.getId() == null) {
			questionBank.setCreateTime(new Date().getTime());
			questionBank.setNumAnswer(0);
			questionBank.setNumCheck(0);
			questionBank.setNumCompletion(0);
			questionBank.setNumJudge(0);
			questionBank.setNumRadio(0);
			questionBank.setNumTotal(0);
			
			questionBankMapper.insert(questionBank);
		} else {
			questionBank.setUpdateTime(new Date().getTime());
			questionBankMapper.updateByPrimaryKey(questionBank);
			
		}
	}
	
	@Override
	public void deleteById(long id) {
		// TODO Auto-generated method stub
		QuestionBank dict = questionBankMapper.selectByPrimaryKey(id);
		if(dict == null) {
			throw new CustomerException("要删除的数据不存在");
		}
		questionBankMapper.deleteByPrimaryKey(id);
	}
}
